#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QLabel>
ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow(parent)
{
    this->setFixedSize(320,600);//配置选择关卡场景
    this->setWindowIcon(QPixmap(":/res/01.png"));//设置图标
    this->setWindowTitle("选择关卡");//设置标题
QMenuBar* bar=menuBar();//创建菜单栏
    setMenuBar(bar);
QMenu* startMenu=bar->addMenu("开始");//创建开始菜单
   QAction* quitAction=startMenu->addAction("退出");//创建退出菜单项
connect(quitAction,&QAction::triggered,[=](){
       this->close();
   });
   MyPushButton* backBtn=new MyPushButton(":/res/back.png");//返回按钮
backBtn->setParent(this);
   backBtn->move(this->width()- backBtn->width(),this->height()-backBtn->height());
connect(backBtn, &MyPushButton::clicked, [=]()
           {
               QTimer::singleShot(500, this, [=](){
               emit this->chooseSceneBack();//返回主场景
           });//延时返回场景
           });
   for(int i=0;i<20;i++)
{
    MyPushButton* menuBtn=new MyPushButton(":/res/level.png");
      menuBtn->setParent(this);
    menuBtn->move(25+i%4*70,150+i/4*70);
      connect(menuBtn,&MyPushButton::clicked,[=]()
{
                QString str=QString("您选择的是第%1关").arg(i+1);
                qDebug()<<str;
                this->hide();//将选关场景隐藏
                play=new PlayScene(i+1);//进入到游戏场景
                play->show();//显示游戏场景
                play->setGeometry(this->geometry());
                connect(play,&PlayScene::chooseSceneBack,[=]()
                        {
                            this->show();
                            this->setGeometry(play->geometry());
                            delete play;
                            play=nullptr;
                        });
});//每个按钮的点击事件
       QLabel* label=new QLabel;
    label->setParent(this);
       label->setFixedSize(menuBtn->width(),menuBtn->height());
    label->setText(QString::number(i+1));
       label->move(25+i%4*70,150+i/4*70);//设置label上的数字大小
    label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);//设置数字的对齐方式
       label->setAttribute(Qt::WA_TransparentForMouseEvents);//鼠标穿透

}//创建选择关卡的按钮
}
void ChooseLevelScene::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QPixmap pix;
pix.load(":/res/scene.png");
painter.drawPixmap(0, 0, this->width(), this->height(), pix);//加载背景
pix.load(":/res/tittle.png");//插入tittle
painter.drawPixmap((this->width()- pix.width()) / 2, 30,pix);//加载标题
}
